#include "Stack.h"

Stack::Stack(int n)
{
	STDatatype* tmp = (STDatatype*)malloc(sizeof(STDatatype) * n);
	if (tmp == NULL)
	{
		perror("mallloc");
		return;
	}
	_arr = tmp;
	_top = 0;
	_capacity = n;
}

void Stack::Push(STDatatype x)
{
	if (_top == _capacity)
	{
		int newcapacity = _capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(_arr, sizeof(STDatatype) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		_arr = tmp;
		_capacity = newcapacity;
	}
	_arr[_top++] = x;
}

int Stack::Empty()
{
	return _top == 0;
}

STDatatype& Stack::Top()
{
	assert(_top>0);
	return _arr[_top - 1];
}

void Stack::Pop()
{
	assert(_top>0);
	_top--;
}


Stack::~Stack()
{
	if (_arr)
	{
		free(_arr);
		_arr = __nullptr;
	}
	_top = _capacity = 0;
}


